package com.myapp.config;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myapp.common.bean.ResultBean;
import com.myapp.common.exception.SysException;
import com.myapp.common.msg.Errors;
import com.myapp.common.msg.MsgInfo;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public ResultBean jsonErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		if(e instanceof SysException) {
			SysException se = (SysException)e;
			MsgInfo<Integer> msg = se.getMsgInfo();
			
			return ResultBean.error(msg);
		}else {
			ResultBean bean = new ResultBean();
			bean.setMsg(e.getMessage());
			bean.setCode(Errors.SYS_ERROR.getCode());
			return bean;
		}
	}

}
